package com.ww.springboot.boot.java8.completablefuture;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-01-20 15:21
 */
public class CompletableFutureDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //有返回值
        CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("运行第一个任务");
            return 1;
        });
        //System.out.println("第一个任务result:" + completableFuture.get());
        System.out.println("1 " + System.currentTimeMillis());
        completableFuture.whenCompleteAsync((j, e) -> {
            System.out.println("完成第一个任务后继续执行....");
            System.out.println("打印上一个任务的结果" + j);
        });
        System.out.println("2 " + System.currentTimeMillis());
        completableFuture.thenAcceptAsync((j) -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("继续异步执行任务");
            System.out.println("打印传值:" + j);
        });
        System.out.println("3 " + System.currentTimeMillis());
        completableFuture.thenAccept((j) -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("继续同步执行任务");
            System.out.println("打印传值:" + j);
        });
        System.out.println("4 " + System.currentTimeMillis());
        completableFuture.thenRun(() -> {
            System.out.println("继续同步执行任务-无传参");
        });
        System.out.println("5 " + System.currentTimeMillis());
        completableFuture.thenApply((j) -> {
            System.out.println("继续同步执行任务-有传参有返回值");
            System.out.println("打印传值：" + j);
            return j;
        });
    }
}
